import * as tslib_1 from "tslib";
/**
 * This class implements interaction with UDF-compatible datafeed.
 * See UDF protocol reference at https://github.com/tradingview/charting_library/wiki/UDF
 */
var UDFCompatibleDatafeed = /** @class */ (function () {
    function UDFCompatibleDatafeed() {
    }
    UDFCompatibleDatafeed.prototype.onReady = function (callback) {
        setTimeout(function () {
            callback({
                supports_search: true,
                supported_resolutions: ['1', '5', '15', '30', '60', '120', '240', '360', '720', '1D', '1W', '1M'],
            });
        });
    };
    UDFCompatibleDatafeed.prototype.resolveSymbol = function (symbolName, onResolve, onError) {
        return tslib_1.__awaiter(this, void 0, void 0, function () {
            return tslib_1.__generator(this, function (_a) {
                setTimeout(function () {
                    onResolve({
                        name: symbolName,
                        ticker: symbolName,
                        full_name: symbolName,
                        description: '',
                        session: '24x7',
                        type: 'bitcoin',
                        exchange: '',
                        listed_exchange: '',
                        timezone: 'Asia/Shanghai',
                        pricescale: Math.pow(10, 8),
                        minmov: 1,
                        has_intraday: true,
                        intraday_multipliers: ['1', '5', '15', '30', '60', '120', '240', '360', '720'],
                        supported_resolutions: ['1', '5', '15', '30', '60', '120', '240', '360', '720', '1D'],
                        has_daily: true,
                        has_weekly_and_monthly: false,
                        has_empty_bars: false,
                    });
                });
                return [2 /*return*/];
            });
        });
    };
    UDFCompatibleDatafeed.prototype.searchSymbols = function (userInput, exchange, symbolType, onResult) { };
    UDFCompatibleDatafeed.prototype.getKline = function (symbol, resolution, from, to) {
        return new Promise(function (resolve, reject) {
            window.ws.onmessage = function (evt) {
                resolve(evt.data);
            };
            window.ws.onerror = function (err) {
                reject(err);
            };
            var params = { "method": "kline.query", "params": [symbol, from, to, (+resolution) * 60], "id": 10 };
            window.ws.send(JSON.stringify(params));
        });
    };
    UDFCompatibleDatafeed.prototype.getBars = function (symbolInfo, resolution, rangeStartDate, rangeEndDate, onResult, onError) {
        return tslib_1.__awaiter(this, void 0, void 0, function () {
            var res, bars;
            return tslib_1.__generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.getKline(symbolInfo.name, resolution, rangeStartDate, rangeEndDate)];
                    case 1:
                        res = _a.sent();
                        if (!res)
                            return [2 /*return*/];
                        res = JSON.parse(res);
                        bars = [];
                        if (res.result.length > 0) {
                            res.result.reduce(function (prev, cur) {
                                bars.push({
                                    time: cur[0] * 1000,
                                    open: +cur[1],
                                    close: +cur[2],
                                    high: +cur[3],
                                    low: +cur[4],
                                    volume: +cur[5],
                                });
                                return prev;
                            }, {});
                        }
                        onResult(bars, { noData: bars.length === 0 });
                        return [2 /*return*/];
                }
            });
        });
    };
    UDFCompatibleDatafeed.prototype.subscribeBars = function (symbolInfo, resolution, onTick, listenerGuid, onResetCacheNeededCallback) {
        window.onRealtimeCallback = onTick;
        window.ws.onmessage = function (evt) {
            var res = JSON.parse(evt.data);
            if (res.params && res.params.length > 0) {
                var cur = res.params[res.params.length - 1];
                onTick({
                    time: cur[0] * 1000,
                    open: +cur[1],
                    close: +cur[2],
                    high: +cur[3],
                    low: +cur[4],
                    volume: +cur[5],
                });
            }
        };
        var params = { "method": "kline.subscribe", "params": [symbolInfo.name, (+resolution) * 60], "id": 20 };
        window.ws.send(JSON.stringify(params));
    };
    UDFCompatibleDatafeed.prototype.unsubscribeBars = function (listenerGuid) {
        var _a = listenerGuid.split('_'), symbol = _a[0], resolution = _a[1];
        var params = { "method": "kline.unsubscribe", "params": [symbol, (+resolution) * 60], "id": 30 };
        window.ws.send(JSON.stringify(params));
    };
    return UDFCompatibleDatafeed;
}());
export { UDFCompatibleDatafeed };
